package com.ptszyxx.popose.module.main.trip.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.trip.TripEntity;

import java.util.HashMap;
import java.util.Map;

public class TripVM extends BaseViewModel<CommonRepository> {

    public TripVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onDetailEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onReleaseCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpTripRelease(this));

    /**
     */
    public void requestList(boolean refresh){
        if (refresh){
            page = 1;
        }else{
            page++;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pageno", String.valueOf(page));
        HttpUtils.getInstance()
                .list(model.tripList(map), this, new OnSuccessListResult<TripEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<TripEntity> response) {
                                uc.onRefreshEvent.setValue(response.getData());
                            }
                        });
    }

    public void jumpTripDetail(TripEntity entity){
        uc.onDetailEvent.setValue(entity);
    }


}
